package BlockingQueue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Exercise {
    public static void main(String[] args) {
        BlockingQueue<Integer> blockingQueue = new LinkedBlockingQueue<>();

        //创建两个线程来作为生产者 和 消费者
        Thread consumer = new Thread(() -> {
            while (true) {
                try {
                    Integer result = blockingQueue.take();
                    System.out.println("消费元素: -> " + result);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        });
        Thread producer = new Thread(() -> {
            int val = 0;
            while (val <= 10) {
                try {
                    blockingQueue.put(val);
                    System.out.println("生产元素: -> " + val);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                val++;
            }
        });
        consumer.start();
        producer.start();
    }
}
